package cn.frank.quartz;

import cn.frank.service.WebSocketService;
import cn.frank.service.oracle.ITJ_JL_DYBD_RIService;
import cn.frank.service.oracle.ITJ_JL_FHBD_RIService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;

/**
 * @author Frank
 * @date 05-28
 */

@Component
public class FrankTask {
    private static final DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");

    @Autowired
    private WebSocketService webSocketService;


    @Autowired
    private ITJ_JL_DYBD_RIService dybdRiService;

    @Autowired
    private ITJ_JL_FHBD_RIService fhbdRiService;

    //当日内调
    //@Scheduled(fixedDelay = 5*1000) // 应用启动后5秒发送初始数据
    @Scheduled(fixedDelayString = "${my.task.inner-data-show-send}") // 应用启动后5秒发送初始数据
    public void sendInnerTjData() {
        System.out.println("发送当日内调数据统计");
        Object data = dybdRiService.findRiDayOfTime();
        System.out.println(data);
        webSocketService.sendMessage("/screen/inner", data);
    }

    //更新当日内调统计数据
    @Scheduled(fixedDelayString = "${my.task.inner-data-show-update}") // 20秒
    public void updateInnerTjData() {
        // 在这里编写统计数据的逻辑
        System.out.println("定时往内调日统计表中插入数据。。。。");
        dybdRiService.executeUpdateData();
    }


    //当日销售
    //@Scheduled(fixedDelay = 5*1000) // 应用启动后5秒发送初始数据
    @Scheduled(fixedDelayString = "${my.task.sale-data-show-send}") // 应用启动后5秒发送初始数据
    public void sendSaleTjData() {
        System.out.println("发送当日销售数据统计");
        Object data = fhbdRiService.findRiDayOfTime();
        System.out.println(data);
        webSocketService.sendMessage("/screen/sale", data);
    }

    //更新当日销售统计数据
    @Scheduled(fixedDelayString = "${my.task.inner-data-show-update}") // 20秒
    public void updateSaleTjData() {
        // 在这里编写统计数据的逻辑
        System.out.println("定时往内调日统计表中插入数据。。。。");
        fhbdRiService.executeUpdateData();
    }
    /**
     * 每日任务：每日凌晨1点fixed-delay-day
     */
    //@Scheduled(cron = "0 0 1 * * ?")
    @Scheduled(cron = "${my.task.fixed-delay-day}")
    public void executeDayTask() {
        System.out.println("每日任务: " + formatter.format(LocalDateTime.now()));
        //Object data = salesDatasService.findSalesData();
        //webSocketService.sendMessage("/screen/day", data);
    }

    /**
     * 每月任务：每月1日1点触发
     */
    //@Scheduled(cron = "0 0 1 1 * ?")
    @Scheduled(cron = "${my.task.fixed-delay-month}")
    public void executeMonthTask() {
        // 这里是你的任务逻辑
        System.out.println("每月任务: " + formatter.format(LocalDateTime.now()));
    }

    /**
     * 每年任务 每年1月1日1点执行
     */
    //@Scheduled(cron = "0 0 1 1 1 *") //
    @Scheduled(cron = "${my.task.fixed-delay-year}") //
    public void executeTask() {
        LocalDateTime now = LocalDateTime.now();
        System.out.println("每年任务: " + now.format(formatter));
        // 在这里编写你的任务逻辑
    }
}
